package me.eg.night;

/**
 * 917. 仅仅反转字母
 * <p>
 * 给定一个字符串 S，返回 “反转后的” 字符串，其中不是字母的字符都保留在原地，而所有字母的位置发生反转。
 * <p>
 * 链接：https://leetcode-cn.com/problems/reverse-only-letters
 */
public class ReverseOnlyLetters {
    public String reverseOnlyLetters(String s) {
        if (s == null) return null;
        int left = 0, right = s.length() - 1;
        char[] chars = s.toCharArray();
        while (left < right) {
            while (left <= right && !Character.isLetter(s.charAt(left))) left++;
            while (left <= right && !Character.isLetter(s.charAt(right))) right--;
            if (left >= right) break;
            char ch = chars[left];
            chars[left] = chars[right];
            chars[right] = ch;
            left++;
            right--;
        }
        return new String(chars);
    }

    public static void main(String[] args) {
        ReverseOnlyLetters ts = new ReverseOnlyLetters();
        System.out.println(ts.reverseOnlyLetters("tNH95P=TV")); // "VTP95H=Nt"
    }
}
